using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class Test_Log_AnswerDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procTest_Log_Answer_add]";
        private string _strSPUpdateName = "dbo.[procTest_Log_Answer_update]";
        private string _strSPDeleteName = "dbo.[procTest_Log_Answer_delete]";
        private string _strSPGetName = "dbo.[procTest_Log_Answer_get]";
        private string _strSPGetName_Selected = "dbo.[procTest_Log_Answer_get_Selected]";
        private string _strSPGetAllName = "dbo.[procTest_Log_Answer_getall]";
        private string _strSPGetPages = "dbo.[procTest_Log_Answer_getpaged]";
        private string _strSPIsExist = "dbo.[procTest_Log_Answer_isexist]";
        private string _strTableName = "[Test_Log_Answer]";
        private string _strSPGetTransferOutName = "dbo.[procTest_Log_Answer_gettransferout]";
        private string _strSPGetByNoName = "procTest_Log_Answer_getno";
        string _strSPGetByNoScoreName = "procTest_Log_Answer_getnoscore";
        #endregion Local Variable

        #region Method
        public Test_Log_AnswerInfo Get(
        Int32 Test_Log_No,
        Int32 Test_Log_Line,
        Int64 Answer_ID,
        ref string sErr)
        {
            Test_Log_AnswerInfo objEntr = new Test_Log_AnswerInfo();
            connect();
            InitSPCommand(_strSPGetName);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), Test_Log_Line);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_ID.ToString(), Answer_ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            if (list.Rows.Count > 0)
                objEntr = (Test_Log_AnswerInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }
        public DataTable Get_Selected(
       Int32 Test_Log_No,
       Int32 Test_Log_Line,

       ref string sErr)
        {
            Test_Log_AnswerInfo objEntr = new Test_Log_AnswerInfo();
            connect();
            InitSPCommand(_strSPGetName_Selected);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), Test_Log_Line);


            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            //if (list.Rows.Count > 0)
            //    objEntr = (Test_Log_AnswerInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            Test_Log_AnswerInfo result = new Test_Log_AnswerInfo();
            result.Test_Log_No = (dt.Rows[i][Test_Log_AnswerInfo.Field.Test_Log_No.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][Test_Log_AnswerInfo.Field.Test_Log_No.ToString()]));
            result.Test_Log_Line = (dt.Rows[i][Test_Log_AnswerInfo.Field.Test_Log_Line.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][Test_Log_AnswerInfo.Field.Test_Log_Line.ToString()]));
            result.Answer_ID = (dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_ID.ToString()] == DBNull.Value ? 0 : Convert.ToInt64(dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_ID.ToString()]));
            result.Answer_Selected = (dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Selected.ToString()] == DBNull.Value ? true : Convert.ToBoolean(dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Selected.ToString()]));
            result.Answer_Order = (dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Order.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Order.ToString()]));
            result.Answer_Text = (dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Text.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][Test_Log_AnswerInfo.Field.Answer_Text.ToString()]));
            result.Rank = (dt.Rows[i][Test_Log_AnswerInfo.Field.Rank.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][Test_Log_AnswerInfo.Field.Rank.ToString()]));

            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(Test_Log_AnswerInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), objEntr.Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), objEntr.Test_Log_Line);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_ID.ToString(), objEntr.Answer_ID);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Selected.ToString(), objEntr.Answer_Selected);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Order.ToString(), objEntr.Answer_Order);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Text.ToString(), objEntr.Answer_Text);
            AddParameter(Test_Log_AnswerInfo.Field.Rank.ToString(), objEntr.Rank);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(Test_Log_AnswerInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), objEntr.Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), objEntr.Test_Log_Line);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_ID.ToString(), objEntr.Answer_ID);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Selected.ToString(), objEntr.Answer_Selected);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Order.ToString(), objEntr.Answer_Order);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_Text.ToString(), objEntr.Answer_Text);
            AddParameter(Test_Log_AnswerInfo.Field.Rank.ToString(), objEntr.Rank);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        Int32 Test_Log_No,
        Int32 Test_Log_Line,
        Int64 Answer_ID
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), Test_Log_Line);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_ID.ToString(), Answer_ID);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(Test_Log_AnswerInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            connect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        Int32 Test_Log_No,
        Int32 Test_Log_Line,
        Int64 Answer_ID
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_No.ToString(), Test_Log_No);
            AddParameter(Test_Log_AnswerInfo.Field.Test_Log_Line.ToString(), Test_Log_Line);
            AddParameter(Test_Log_AnswerInfo.Field.Answer_ID.ToString(), Answer_ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(Test_Log_AnswerInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public string Delete_TestNo(int maxNo)
        {
            string result = "";
            string query = "Delete  from " + _strTableName + "where Test_Log_No = " + maxNo;
            connect();
            try
            {
                executeNonQuery(query);
            }
            catch (Exception ex) { result = ex.Message; }
            disconnect();
            return result;
        }

        public DataTable GetLogByNo(long No, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetByNoName);
            AddParameter("No", No);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }



        public DataTable GetLogByNoScore(int no, double score, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetByNoScoreName);
            AddParameter("No", no);
            AddParameter("Score", score);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
